﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using LorraineSec.DatabaseLib;

namespace LorraineSec.DatabaseLib.DataObjects
{
    public class Device
    {
        #region Constants
        private const string ROOT_STR = XmlDatabase.DEVICE;
        private const string ID_STR = XmlDatabase.ID;
        private const string LIB_STR = XmlDatabase.LIBID;
        private const string COMP_STR = XmlDatabase.COMPID;
        private const string NAME_STR = XmlDatabase.NAME;
        private const string TYPE_STR = XmlDatabase.TYPE;
        private const string SERIAL_STR = XmlDatabase.SERIAL;
        #endregion

        private XmlNode xmlDeviceRoot;
        private XmlNode xmlLibID;
        private XmlNode xmlCompartmentID;
        private XmlNode xmlName;
        private XmlNode xmlType;
        private XmlNode xmlSerial;

        public Device(XmlNode node)
        {
            xmlDeviceRoot = node;    
            xmlLibID = xmlDeviceRoot.SelectSingleNode(LIB_STR);
            xmlCompartmentID = xmlDeviceRoot.SelectSingleNode(COMP_STR);
            xmlName = xmlDeviceRoot.SelectSingleNode(NAME_STR);
            xmlType = xmlDeviceRoot.SelectSingleNode(TYPE_STR);
            xmlSerial = xmlDeviceRoot.SelectSingleNode(SERIAL_STR);
        }

        public static List<Device> GetList(XmlDatabase db)
        {
            List<Device> retList = new List<Device>();
            if (db.devices != null)
            {
                XmlNodeList nodeList = db.devices.SelectNodes(ROOT_STR);
                retList.Capacity = nodeList.Count;
                foreach (XmlNode n in nodeList)
                {
                    retList.Add(new Device(n));
                }            
            }
            return retList;
        }

        public static implicit operator Device(XmlNode node)
        {
            return new Device(node);
        }

        public static explicit operator XmlNode(Device u)
        {
            return u.xmlDeviceRoot;
        }

        public uint ID
        {
            get { return UInt32.Parse(xmlDeviceRoot.Attributes[ID_STR].Value); }
        }

        public byte LibID
        {
            get { return Byte.Parse(xmlLibID.InnerText); }
            set { xmlLibID.InnerText = value.ToString(); }
        }

        public uint CompartmentID
        {
            get { return UInt32.Parse(xmlCompartmentID.InnerText); }
            set { xmlCompartmentID.InnerText = value.ToString(); }
        }

        public string Name
        {
            get { return xmlName.InnerText; }
            set { xmlName.InnerText = value; }
        }

        public string Type
        {
            get { return xmlType.InnerText; }
        }

        public string Serial
        {
            get { return xmlSerial.InnerText; }
        }

        public override string ToString()
        {
            return Name;
        }

    }
}
